﻿using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace Clothes.Items.ModuleData
{

    #region RUCategoriesByFather

    public class RUCategoriesByFather : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string father;

        public RUCategoriesByFather(bool updater = false)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = CL_Categories.Name;

            QueryBuilder qb = new QueryBuilder();

            sqlP1 = new SqlABParameter("@p51", CL_Categories.CodeFather);

            qb.AddSelect();
            qb.AddFrom(CL_Categories.Name);
            qb.AddCompare(CL_Categories.CodeFather, sqlP1);
            qb.AddOrder(CL_Categories.Description);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = father;
        }

        public bool Find(string father = "")
        {
            this.father = father;
            return base.Find();
        }
    }

    #endregion
    #region RUCategories

    public class RUCategories : DataReaderUpdater
    {
        public RUCategories(bool updater = false)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = CL_Categories.Name;

            QueryBuilder qb = new QueryBuilder();

            qb.AddSelect();
            qb.AddFrom(CL_Categories.Name);
            qb.AddOrder(CL_Categories.Description);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion
    #region DRCategories

    public class DUCategories : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string code;

        public DUCategories(bool updater = false)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = CL_Categories.Name;

            QueryBuilder qb = new QueryBuilder();

            sqlP1 = new SqlABParameter("@p51", CL_Categories.Code);

            qb.AddSelect();
            qb.AddFrom(CL_Categories.Name);
            qb.AddCompare(CL_Categories.Code, sqlP1);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = code;
        }

        public bool Find(string code)
        {
            this.code = code;
            return base.Find();
        }
    }

    #endregion
    #region RUItemsContainsCategory

    public class RUItemsContainsCategory : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string cat;

        public RUItemsContainsCategory(bool updater = false)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            sqlP1 = new SqlABParameter("@p51", typeof(string), 4);

            myTable = CL_Items.Name;

            QueryBuilder qb = new QueryBuilder();

            qb.AddSelect();
            qb.AddFrom(CL_Items.Name);
            qb.AddLike(CL_Items.Category, sqlP1);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = cat;
        }

        public bool Find(string cat)
        {
            this.cat = string.Format("%{0}%", cat);
            return base.Find();
        }
    }

    #endregion



    #region DUItem

    public class DUItem : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        int ID;

        public DUItem(bool updater = false)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = CL_Items.Name;

            QueryBuilder qb = new QueryBuilder();

            qb.AddSelect();
            qb.AddFrom(CL_Items.Name);

            sqlP1 = new SqlABParameter("@p51", CL_Items.ID);
            qb.AddCompare(CL_Items.ID, sqlP1);

            qb.AddOrder(CL_Items.Description);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = ID;
        }

        public bool Find(int id)
        {
            this.ID = id;
            return base.Find();
        }
    }

    #endregion
}